﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fiddler;

namespace WCATScenarioGenerator
{
    [assembly: RequiredVersion("2.2.2.0")]
    public class WCATScenarioGenerator : IAutoTamper , IHandleExecAction
    {
        List<StringBuilder> transactions;

        public WCATScenarioGenerator()
        {
            resetWCATScenario();

        }
        #region Implementation of IFiddlerExtension

        public void OnLoad()
        {
   
        }

        public void OnBeforeUnload()
        {
            
        }

        public bool OnExecAction(string sCommand)
        {
            var oParams = Utilities.Parameterize(sCommand);
            if (oParams.Length < 1 || oParams[0].ToLower() != "wcat")
                return false;

            var command = oParams.GetPartOrDefault(1,"").ToLower();
            switch(command)
            {
                case "reset":
                    resetWCATScenario();
                    break;
                case "addtrans":
                    addNewTransaction(oParams.GetPartOrDefault(2,string.Empty));
                    break;
                case "save":
                    saveWCATScenarioFile(oParams.GetPartOrDefault(2,"fiddler.wcat"));
                    break;
                default:
                    MessageBox.Show(string.Format("Command '{0}' not understood", command));
                    break;
            }
            
            return true;
        }

        private void resetWCATScenario()
        {
            transactions = new List<StringBuilder>();
        }

        private void saveWCATScenarioFile(string filename)
        {
            using (var sw = new System.IO.StreamWriter(filename))
            {
                var sb = new StringBuilder();

                sb.AppendLine("scenario {");
                sb.AppendLine("\twarmup\t= 300;");
                sb.AppendLine("\tduration\t= 300;");
                sb.AppendLine("\tcooldown\t= 30;");

                //default block
                sb.AppendLine("\tdefault {");
                sb.AppendLine("\t\tsetheader {");
                sb.AppendLine("\t\t\tname\t= \"Connection\";");
                sb.AppendLine("\t\t\tvalue\t= \"keep-alive\";");
                sb.AppendLine("\t\t}");
                sb.AppendLine();
                sb.AppendLine("\t\tversion\t= HTTP11;");
                sb.AppendLine("\t\tstatuscode\t= 0;");
                //sb.AppendLine("\t\tclose\t= kq;");
                sb.AppendLine("\t}");
                sb.AppendLine();

                //transactions

                foreach(var transaction in transactions)
                {
                    sb.Append(transaction.ToString());
                }

                sb.AppendLine("}");

                sw.Write(sb.ToString());
            }
        }


        private void addNewTransaction(string id)
        {
            var sbResult = new StringBuilder();
            var oSessions = FiddlerApplication.UI.GetSelectedSessions();
            sbResult.AppendLine("\ttransaction {");
            sbResult.AppendLine("\t\tid = \"test\";");
            sbResult.AppendLine("\t\tweight = 1000;");

            foreach (var session in oSessions)
            {
                sbResult.Append(session.toWCATRequest());
            }
            sbResult.AppendLine("\t}");

            transactions.Add(sbResult);

            //MessageBox.Show(sbResult.ToString());
        }

        #endregion

        #region Implementation of IAutoTamper

        public void AutoTamperRequestBefore(Session oSession)
        {

        }

        public void AutoTamperRequestAfter(Session oSession)
        {

        }

        public void AutoTamperResponseBefore(Session oSession)
        {
        }

        public void AutoTamperResponseAfter(Session oSession)
        {

        }

        public void OnBeforeReturningError(Session oSession)
        {

        }

        #endregion
    }
}

